package com.shujia;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo1 {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, NoSuchFieldException {

        // 通过new关键字创建对象，实际上内部是调用了类对象的构造方法
        Student student = new Student("001", "张三", "23");


        // 反射
        /**
         * 获取类对象
         * 1、Student.class
         *
         * java中每一个类的类对象是Class这个类的一个对象
         *
         * 可以通过类对象获取类的方法，属性和构造函数
         *
         */
        Class<Student> studentClass = Student.class;
        //通过类全名获取类对象
        Class<?> aClass = Class.forName("com.shujia.Student");
        //通过类的对象获取类对象
        Class<?> aClass1 = student.getClass();

        System.out.println(studentClass);
        System.out.println(aClass);
        System.out.println(aClass1);

        //调用无参构造方法
        Student student2 = studentClass.newInstance();

        //Class<Class> classClass = Class.class;

        //通过反射获取构造函数
        Constructor<Student> constructor = studentClass.getConstructor(String.class, String.class);

        //创建对象
        Student student1 = constructor.newInstance("002", "李四");

        System.out.println(student1);

        //获取属性
        Field[] fields = studentClass.getFields();

        Field name = studentClass.getDeclaredField("id");


        //获取方法
        Method toString = studentClass.getMethod("toString");

        //执行方法
        Object invoke = toString.invoke(student1);


        // 获取注解
        Annotation[] annotations = studentClass.getAnnotations();


    }
}
